<template>
  <view class="product-page">
    <u-navbar placeholder
              leftIcon="arrow-left"
              :title="$t('pages_index_index_msg_80f2bb5b')"
              @leftClick="goBack" />
    
    <!-- 搜索栏 -->
    <view class="search-bar">
      <u-search 
        v-model="searchKeyword" 
        :placeholder="$t('pages_product_list_list_msg_2470ef19')" 
        @search="onSearch"
        @clear="onClearSearch"
        :showAction="false"
      />
    </view>
    
    <mescroll-body
      ref="mescrollRef"
      @init="mescrollInit"
      @down="downCallback"
      @up="upCallback"
      :down="downOption"
      :up="upOption"
      :height="`${systemInfo.windowHeight - systemInfo.statusBarHeight - 44 - 80}px`"
    >
      <view class="product-list">
        <view v-for="item in dataList" 
              :key="item.id" 
              class="product-item"
              @click="onProductClick(item)">
          <image :src="item.pic || '/static/images/default-product.png'" 
                 class="product-image" 
                 mode="aspectFill" />
          <view class="product-content">
            <text class="product-name">{{ item.name }}</text>
            <view class="product-meta">
              <text class="product-price">¥{{ item.price || '0.00' }}</text>
              <text class="product-id">ID: {{ item.id }}</text>
            </view>
          </view>
        </view>
      </view>
    </mescroll-body>
  </view>
</template>

<script>
import { mapState } from 'vuex'
import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js'
import productApi from '@/api/modules/product'

export default {
  name: 'Product-List',
  mixins: [MescrollMixin],
  computed: {
    ...mapState({
      systemInfo: (state) => state.app.systemInfo,
    }),
  },
  data() {
    return {
      searchKeyword: '',
      dataList: [],
      downOption: {
        auto: true,
      },
      upOption: {
        auto: false,
      },
    }
  },
  onLoad(options) {
    // mescroll会自动触发upCallback
  },
  methods: {
    goBack() {
      uni.navigateBack()
    },
    
    onSearch() {
      // 重新加载数据
      this.mescroll.resetUpScroll()
    },
    
    onClearSearch() {
      this.searchKeyword = ''
      this.mescroll.resetUpScroll()
    },
    
    onProductClick(item) {
      // 跳转到商品详情页
      this.$utils.router({
        url: `/pages/product/detail/detail?id=${item.id}`
      })
    },
    
    async upCallback(page) {
      try {
        const params = {
          page: page.num - 1, // API使用0基索引，mescroll从1开始
          size: page.size,
          keyword: this.searchKeyword
        }
        
        const res = await productApi.getList(params)
        console.log('商品数据:', res);
        
        // 根据实际API响应结构处理数据
        const productData = res.content || []
        
        if (page.num == 1) {
          this.dataList = productData
        } else {
          this.dataList = this.dataList.concat(productData)
        }
        
        // 结束加载，传入实际数据长度和总页数
        const totalPages = res.totalPages || 1
        this.mescroll.endByPage(productData.length, totalPages)
      } catch (error) {
        console.error('获取商品列表失败:', error)
        this.mescroll.endErr()
      }
    },
  }
}
</script>

<style lang="scss" scoped>
.product-page {
  background: #f5f5f5;
  min-height: 100vh;
}

.search-bar {
  background: #fff;
  padding: 20rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.product-list {
  padding: 20rpx;
}

.product-item {
  background: #fff;
  border-radius: 20rpx;
  overflow: hidden;
  box-shadow: 0 8rpx 20rpx rgba(0, 0, 0, 0.04);
  display: flex;
  height: 160rpx;
  margin-bottom: 20rpx;
  transition: all 0.2s ease;
  
  &:active {
    transform: scale(0.98);
  }
}

.product-image {
  width: 200rpx;
  height: 160rpx;
  flex-shrink: 0;
}

.product-content {
  flex: 1;
  padding: 20rpx;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.product-name {
  font-size: 28rpx;
  font-weight: 600;
  color: #333;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.product-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12rpx;
}

.product-price {
  font-size: 26rpx;
  font-weight: bold;
  color: #ff4d4f;
}

.product-id {
  font-size: 22rpx;
  color: #999;
}
</style>